﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using System.Data.SQLite;
using System.IO;
using SharedClientDll;
using Shared.Method;
using Newtonsoft.Json;
//using Newtonsoft.Json;

namespace OfflineModemCheck
{
    public partial class FormCheck : Form
    {
        //private string _localPath = Application.StartupPath + "\\Data\\";

        private string _localIp;
        private int _localPort;
        private string _modemIp;

        //private int _formNum;
        //private Point _formPoint;

        public FormCheck()
        {
            //_formNum = formNum;
            //_formPoint = formPoint;
            InitializeComponent();

            //this.Location = formPoint;
        }

        private void FormCheck_Load(object sender, EventArgs e)
        {
            try
            {
                _localIp = Shared.Method.AppSetting.GetValue("LocalIp");
                _localPort = int.Parse(Shared.Method.AppSetting.GetValue("LocalPort"));
                _modemIp = Shared.Method.AppSetting.GetValue("IpAddress");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Close();
            }
        }

        //private DataTable table = null;
        private List<DEVICE_DATA> _listData = null;
        private bool _runing = false;

        private void ButtonCheck_Click(object sender, EventArgs e)
        {
            if (_listData == null || _listData.Count == 0)
            {
                MessageBox.Show("请先导入数据");
                return;
            }

            //修改状态
            if (_runing)
            {
                _runing = false;
                this.buttonCheck.Enabled = false;
                this.buttonCheck.Text = "开始检查";
            }
            else
            {
                _runing = true;
                this.buttonCheck.Text = "结束检查";
                WorkTask();
            }
        }

        private async void WorkTask()
        {
            this.buttonImport.Enabled = false;

            await Task.Run(() =>
            {
                while (_runing)
                {
                    Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "等待光猫断开连接");
                    //Shared.SoftMethod.SoftShared.SetControl(this, this.LabelTestState, "WAIT", Color.Black);

                    while (PingBaseClass.PingTest(_localIp, _modemIp))
                    {
                        System.Threading.Thread.Sleep(500);
                    }

                    //输入MAC地址
                    SharedClientDll.FormShared.FormEnterMsg enterMsg = new SharedClientDll.FormShared.FormEnterMsg("请输入MAC：",this.Location);
                    enterMsg.ShowDialog();

                    string enterMac = enterMsg.GetEnter();

                    Shared.SoftMethod.SoftShared.SetControl(this, this.richTextBoxMsg, string.Empty);

                    //检查光猫是否连接
                    Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "等待连接……");
                    //SetTestResult(true, "WAITFOR");
                    Shared.SoftMethod.SoftShared.SetControl(this, this.LabelTestState, "WAITFOR", Color.Black);
                    //连续PING通4次才算连接上了

                    int pingOkTimes = 0;
                    for (int i = 0; i < 20; i++)
                    {
                        if (PingBaseClass.PingTest(_localIp, _modemIp))
                        {
                            pingOkTimes++;
                            if (pingOkTimes >= 4)
                                break;
                        }
                        else
                        {
                            pingOkTimes = 0;
                        }

                        System.Threading.Thread.Sleep(500);
                    }

                    if (pingOkTimes < 4)
                    {
                        Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "未能成功PING通光猫");
                        continue;
                    }

                    //显示正在测试当中
                    //SetTestResult(false, "TESTING");
                    Shared.SoftMethod.SoftShared.SetControl(this, this.LabelTestState, "TESTING", Color.Black);

                    //光猫真实MAC
                    string realMac = string.Empty;
                    for (int i = 0; i < 3; i++)
                    {
                        realMac = ArpBaseClass.GetMacFromIp(_localIp, _modemIp);
                        if (!string.IsNullOrEmpty(realMac) && realMac.Length == 12) break;
                    }

                    //获取了MAC地址，将其写在界面上
                    if (string.IsNullOrEmpty(realMac) == false)
                    {
                        Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "获取MAC地址成功_ARP，MAC：" + realMac);
                    }

                    //判断内外MAC是否一致
                    if (realMac != enterMac)
                    {
                        Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, $"MAC不一致，光猫MAC：{realMac}，扫码MAC：{enterMac}");
                        MessageBox.Show("内外MAC不一致");
                        continue;
                    }


                    //获取产品的校验信息
                    var device = _listData.Where(a => a.MAC == realMac).FirstOrDefault();

                    if (device == null)
                    {
                        Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "获取检查数据失败");
                        continue;
                    }

                    Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "等待27秒……");
                    System.Threading.Thread.Sleep(27000);

                    //网页数据重测次数3次，等待5秒
                    string httpMsg = null;

                    for (int i = 0; i < 5; i++)
                    {
                        Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "等待时间15秒……");
                        System.Threading.Thread.Sleep(15000);

                        if (device.URL.Contains("CMCC"))
                        {
                            Shared.SoftMethod.SoftShared.LoginCMCC(_modemIp, _localIp, _localPort, ref portOffset, device.PROVINCE);
                        }


                        httpMsg = GetModemMessage(device.URL);
                        if (string.IsNullOrEmpty(httpMsg) || httpMsg.Length < 100)
                        {
                            Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "未能获得光猫配置数据");
                        }
                        else
                        {
                            Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "获取光猫配置数据成功");
                            break;//成功获取网页数据
                        }
                    }

                    Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, httpMsg);

                    Dictionary<string, string> listValue = GetDictionary(httpMsg);//从网页上获取需要检查的信息

                    bool checkRet = CheckDeviceMsg(device, listValue, out string checkMsg);

                    if (checkRet)
                    {
                        Shared.SoftMethod.SoftShared.SetControl(this, this.LabelTestState, "PASS", Color.Green);
                    }
                    else
                    {
                        Shared.SoftMethod.SoftShared.SetControl(this, this.LabelTestState, "FAIL", Color.Red);

                    }
                }

            });

            this.buttonImport.Enabled = true;
            this.buttonCheck.Enabled = true;
        }

        private void ButtonImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog
            {
                ReadOnlyChecked = true
            };
            dialog.ShowDialog();

            var table = YcdExcelStatic.ExcelToTable(dialog.FileName, out string msg);

            if (table != null && table.Rows.Count >= 1)
            {
                MessageBox.Show($"导入数据成功，数据量为{table.Rows.Count}");

                //将datatbale转换成list<T>
                _listData = JsonConvert.DeserializeObject<List<DEVICE_DATA>>(JsonConvert.SerializeObject(table));

                this.LabelTestState.Text = $"数据量为：{table.Rows.Count}";
            }
            else
            {
                MessageBox.Show("导入数据失败");
            }
        }


        #region 出厂检查

        private int portOffset = DateTime.Now.Second;
        /// <summary>
        /// 获取网页数据
        /// </summary>
        /// <returns></returns>
        private string GetModemMessage(string URL)
        {
            //使用新的类YcdTelnetClass
            if (portOffset >= 800) portOffset = 0;
            portOffset++;

            string webKey = string.Empty;

            HttpArgs httpArgs = new HttpArgs
            {
                Url = "/cgi-bin/cgic_systeminfo.cgi",
            };

            if (URL.Contains("_CU"))
            {
                httpArgs.Port = 80;
                httpArgs.Host = "192.168.1.1:80";
                webKey = "java";
            }
            else if (URL.Contains("CMCC"))
            {
                httpArgs.Port = 80;
                httpArgs.Host = "192.168.1.1:80";
                webKey = "0,0,0,";
            }
            else
            {
                httpArgs.Port = 8080;
                httpArgs.Host = "192.168.1.1:8080";
                webKey = "1,1,1,1,";
            }

            //使用新的方法获取网页数据
            bool hasKey = HttpBaseClass.GetHttpMsg(_localIp, _localPort + portOffset, _modemIp, httpArgs.Port, httpArgs, webKey, out string modemMsg);

            System.Threading.Thread.Sleep(300);

            if (hasKey == false) return string.Empty;

            return modemMsg;
        }

        /// <summary>
        /// 检查产品相关的出厂信息得出结果
        /// </summary>
        /// <param name="device"></param>
        /// <param name="dict"></param>
        /// <returns></returns>
        private bool CheckDeviceMsg(DEVICE_DATA device, Dictionary<string, string> dict, out string message)
        {
            message = "";

            if (device == null || dict == null) { message = "升级固件,解析网页数据失败"; return false; }

            //写入信息到测试结果中

            //youhua_add_180102
            if (dict.TryGetValue("Manufacturer", out string value) && value == device.Manufacturer)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Manufacturer检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Manufacturer检查未通过");
                message = "恢复工厂重走信息检";
                return false;
            }

            //产品型号
            if (dict.TryGetValue("ProductClass", out value) && value == device.ProductClass)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "ProductClass检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "ProductClass检查未通过");
                message = "恢复工厂重走信息检";
                return false;
            }

            //硬件版本
            if (dict.TryGetValue("HWVersion", out value) && value == device.HWVersion)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "HWVersion检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "HWVersion检查未通过");
                message = "恢复工厂重走信息检";
                return false;
            }

            //软件版本
            if (dict.TryGetValue("SWVersion", out value) && value == device.SWVersion)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "SWVersion检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "SWVersion检查未通过");
                message = "固件升级,固件版本检查失败";
                return false;
            }

            //省份信息就检查
            if ((dict.TryGetValue("PROVINCE", out value) == false || string.IsNullOrEmpty(value))
                && string.IsNullOrEmpty(device.PROVINCE))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页中均无省份信息，未检查");
            }
            else if (dict.TryGetValue("PROVINCE", out value) && value == device.PROVINCE)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PROVINCE检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PROVINCE检查未通过");
                message = "恢复工厂重走信息检";
                return false;
            }


            //MAC
            if (dict.TryGetValue("MAC", out value) && value == device.MAC)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "MAC检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "MAC检查未通过");
                message = "MAC地址检查失败";
                return false;
            }

            //OUI1
            //if (device.Devicedevice.Contains("_RC") == false && device.Devicedevice.Contains("PT926") == false)
            //{
            //兼容9603C版本
            if (dict.TryGetValue("OUI", out value) && value == device.OUI)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "OUI检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "OUI检查未通过");
                message = "恢复工厂重走信息检,OUI检查失败";
                return false;
            }
            //}

            //OUI2
            if (dict.TryGetValue("SerialNum", out value) && value == device.SerialNum)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "SerialNum检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "SerialNum检查未通过");
                message = "恢复工厂重走信息检";
                return false;
            }
            //xponMode
            if (dict.TryGetValue("xponMode", out value) && value == device.xponMode)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "xponMode检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "xponMode检查未通过");
                message = "检查混料,XPONMode检查失败";
                return false;
            }


            //省份信息就检查
            if ((dict.TryGetValue("GPON_SN", out value) == false || string.IsNullOrEmpty(value))
                && string.IsNullOrEmpty(device.GPON_SN))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页中均无GPON_SN，未检查");
            }
            else if (dict.TryGetValue("GPON_SN", out value) && value == device.GPON_SN)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PROVINCE检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "GPON_SN检查未通过");
                message = "恢复工厂重走信息检";
                return false;
            }



            //Mode_只有R方案检查
            if (dict.TryGetValue("Mode", out value) && value == device.Mode)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Mode检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Mode检查未通过");
                message = "检查混料,MODE检查未通过";
                return false;
            }

            //username
            if (dict.TryGetValue("username", out value) && value == device.username)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "username检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "username检查未通过");
                message = "恢复工厂重走信息检,username未通过";
                return false;
            }
            //password
            if (dict.TryGetValue("web_passwd", out value) && value == device.web_passwd)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "web_passwd检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "web_passwd检查未通过");
                message = "恢复工厂重走信息检,web_passwd未通过";
                return false;
            }
            //SSID1
            if (dict.TryGetValue("SSID1", out value) && value == device.SSID1)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "SSID1检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "SSID1检查未通过");
                message = "恢复工厂重走信息检,username未通过";
                return false;
            }
            //SSID PWD
            if (dict.TryGetValue("WPAPSK1", out value) && value == device.WPAPSK1)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "WPAPSK1检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "WPAPSK1检查未通过");
                message = "恢复工厂重走信息检,WPAPSK1未通过";
                return false;
            }

            //SSID2检查规则，双向对比
            if (string.IsNullOrEmpty(device.SSID2)
                && (dict.TryGetValue("SSID2", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和光猫上SSID2都为空，不用检查");
            }
            else

                if (dict.TryGetValue("SSID2", out value) && value == device.SSID2 && string.IsNullOrEmpty(value) == false)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "SSID2检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "SSID2检查未通过");
                message = "恢复工厂重走信息检,SSID2未通过";
                return false;
            }

            //SSID 2 PWD
            if (string.IsNullOrEmpty(device.WPAPSK2)
                && (dict.TryGetValue("WPAPSK2", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和光猫上WPAPSK2都为空，不用检查");
            }
            else if (dict.TryGetValue("WPAPSK2", out value) && value == device.WPAPSK2 && string.IsNullOrEmpty(value) == false)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "WPAPSK2检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "WPAPSK2检查未通过");
                message = "恢复工厂重走信息检,WPAPSK2未通过";
                return false;
            }

            //light_model
            if (string.IsNullOrEmpty(device.light_model)
                && (dict.TryGetValue("light_model", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和光猫上light_model都为空，不用检查");
            }
            else if (dict.TryGetValue("light_model", out value) && value == device.light_model && string.IsNullOrEmpty(value) == false)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "light_model检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "light_model检查未通过");
                message = "恢复工厂重走信息检,light_model未通过";
                return false;
            }

            //light_sn
            if (string.IsNullOrEmpty(device.light_sn)
                && (dict.TryGetValue("light_sn", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和光猫上light_sn都为空，不用检查");
            }
            else if (dict.TryGetValue("light_sn", out value) && value == device.light_sn && string.IsNullOrEmpty(value) == false)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "light_sn检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "light_sn检查未通过");
                message = "恢复工厂重走信息检,light_sn未通过";
                return false;
            }


            //IP
            if (string.IsNullOrEmpty(device.IP)
                && (dict.TryGetValue("IP", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页均无IP项目，未检查");
            }
            else if (dict.TryGetValue("IP", out value) && value == device.IP)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "IP检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "IP检查未通过");
                message = "固件升级";
                return false;
            }


            //wanTelnet
            if (dict.TryGetValue("WanTelnet", out value) && value == device.WlanTelnet)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Wan Telnet检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Wan Telnet检查未通过");
                message = "恢复出厂设置";
                return false;
            }

            //LanTelnet
            if (dict.TryGetValue("LanTelnet", out value) && value == device.LanTelnet)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Lan Telnet检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Lan Telnet检查未通过");
                message = "恢复出厂设置";
                return false;
            }

            //checksum，信息校验，R方案需要检查checkSum

            if (dict.TryGetValue("securitycheck", out value) && value == device.securitycheck)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "security check检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "security check 检查未通过");
                message = "恢复工厂重走信息检,信息安全检查未通过";
                return false;
            }

            //BOB4
            if (string.IsNullOrEmpty(device.BoBtable4check)
                && (dict.TryGetValue("BoBtable4check", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无BoBtable4check，未检查");
            }
            else
                if (dict.TryGetValue("BoBtable4check", out value) && value == device.BoBtable4check)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "BoBtable4check检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "BoBtable4check 检查未通过");
                message = "恢复工厂重走信息检,信息安全检查未通过";
                return false;
            }

            //BOB5
            if (string.IsNullOrEmpty(device.BoBtable5check)
                && (dict.TryGetValue("BoBtable5check", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无BoBtable5check，未检查");
            }
            else if (dict.TryGetValue("BoBtable5check", out value) && value == device.BoBtable5check)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "BoBtable5check检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "BoBtable5check 检查未通过");
                message = "恢复工厂重走信息检,信息安全检查未通过";
                return false;
            }

            //PROCESS NAME
            if (string.IsNullOrEmpty(device.ProcessName)
                && (dict.TryGetValue("ProcessName", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无ProcessName，未检查");
            }
            else
                if (dict.TryGetValue("ProcessName", out value) && value.Contains(device.ProcessName))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "ProcessName检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "ProcessName检查未通过");
                message = "恢复工厂重走信息检,信息安全检查未通过";
                return false;
            }


            //WanConnect
            if (string.IsNullOrEmpty(device.WanConnect)
                && (dict.TryGetValue("WanConnect", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无WanConnect，未检查");
            }
            else
                if (dict.TryGetValue("WanConnect", out value) && value == device.WanConnect)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "WanConnect检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "WanConnect检查未通过");
                message = "固件升级，WanConnect检查未通过";
                return false;
            }


            //PluginNum

            if (string.IsNullOrEmpty(device.PluginNum)
                && (dict.TryGetValue("PluginNum", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无PluginNum，未检查");
            }
            else
                if (dict.TryGetValue("PluginNum", out value) && value == device.PluginNum)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PluginNum检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PluginNum检查未通过");
                message = "插件升级，PluginNum检查未通过";
                return false;
            }


            //pluginName
            if (string.IsNullOrEmpty(device.PluginName)
                && (dict.TryGetValue("PluginName", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无PluginName，未检查");
            }
            else if (dict.TryGetValue("PluginName", out value) && value == device.PluginName)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PluginName检查通过");
            }
            else if (value.Length == device.PluginName.Length)
            {
                string[] items = value.Split(',');
                foreach (string i in items)
                {
                    if (device.PluginName.Contains(i) == false)
                    {
                        message = "插件升级，PluginName检查未通过";
                        return false;
                    }
                }
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PluginName检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PluginName检查未通过");
                message = "插件升级，PluginName检查未通过";
                return false;
            }

            //pluginRun
            if (string.IsNullOrEmpty(device.PluginRun)
                && (dict.TryGetValue("PluginRun", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无PluginRun，未检查");
            }
            else
                if (dict.TryGetValue("PluginRun", out value) && value == device.PluginRun)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PluginRun检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "PluginRun检查未通过");
                message = "插件升级，PluginRun检查未通过";
                return false;
            }

            //Framework1
            if (string.IsNullOrEmpty(device.FrameWork1)
                && (dict.TryGetValue("Framework1", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无Framework1，未检查");
            }
            else
                if (dict.TryGetValue("Framework1", out value) && value == device.FrameWork1)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Framework1检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Framework1检查未通过");
                message = "插件升级，Framework1检查未通过";
                return false;
            }

            //Framework2
            if (string.IsNullOrEmpty(device.FrameWork2)
                && (dict.TryGetValue("Framework2", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无Framework2，未检查");
            }
            else
                if (dict.TryGetValue("Framework2", out value) && value == device.FrameWork2)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Framework2检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Framework2检查未通过");
                message = "插件升级,Framework2检查未通过";
                return false;
            }

            //APPS
            if (string.IsNullOrEmpty(device.APPS)
                && (dict.TryGetValue("APPS", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无APPS，未检查");
            }
            else
                if (dict.TryGetValue("APPS", out value) && value == device.APPS)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "APPS检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "APPS检查未通过");
                message = "插件升级，APPS检查未通过";
                return false;
            }


            //Framework(active)
            if (string.IsNullOrEmpty(device.FrameworkActive)
              && (dict.TryGetValue("Framework(active)", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无Framework(active)，未检查");
            }
            else
              if (dict.TryGetValue("Framework(active)", out value) && value == device.FrameworkActive)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Framework (active)检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Framework (active)检查未通过");
                message = "插件升级，Framework (active)检查未通过";
                return false;
            }


            //URL
            if (string.IsNullOrEmpty(device.URL)
                && (dict.TryGetValue("URL", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无URL，未检查");
            }
            else
                if (dict.TryGetValue("URL", out value) && value == device.URL)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "URL检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "URL检查未通过");
                message = "固件升级，URL检查未通过";
                return false;
            }

            //RelativeURL
            if (string.IsNullOrEmpty(device.RelativeURL)
                && (dict.TryGetValue("RelativeURL", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无RelativeURL，未检查");
            }
            else
                if (dict.TryGetValue("RelativeURL", out value) && value == device.RelativeURL)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Relative URL检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Relative URL检查未通过");
                message = "固件升级，Relative URL检查未通过";
                return false;
            }

            //LastChangedRev
            if (string.IsNullOrEmpty(device.LastChangedRev)
                && (dict.TryGetValue("LastChangedRev", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无LastChangedRev，未检查");
            }
            else
                if (dict.TryGetValue("LastChangedRev", out value) && value == device.LastChangedRev)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Last Changed Rev检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Last Changed Rev检查未通过");
                message = "固件升级，Last Changed Rev检查未通过";
                return false;
            }

            //LastChangedData
            if (string.IsNullOrEmpty(device.LastChangedDate)
                && (dict.TryGetValue("LastChangedDate", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无LastChangedDate，未检查");
            }
            else
                if (dict.TryGetValue("LastChangedDate", out value) && value == device.LastChangedDate)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Last Changed Date检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Last Changed Date检查未通过");
                message = "固件升级，Last Changed Date检查未通过";
                return false;
            }

            //Compile Time
            if (string.IsNullOrEmpty(device.CompileTime)
                && (dict.TryGetValue("CompileTime", out value) == false || string.IsNullOrEmpty(value)))
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "数据库和网页上均无CompileTime，未检查");
            }
            else
                if (dict.TryGetValue("CompileTime", out value) && value == device.CompileTime)
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Compile Time检查通过");
            }
            else
            {
                Shared.SoftMethod.SoftShared.AppendText(this, this.richTextBoxMsg, "Compile Time检查未通过");
                message = "固件升级，Compile Time检查未通过";
                return false;
            }


            message = "检查通过";

            return true;
        }

        /// <summary>
        /// 将光猫数据从字符串转换为键值对
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        private Dictionary<string, string> GetDictionary(string msg)
        {
            if (string.IsNullOrEmpty(msg)) return null;

            var dict = new Dictionary<string, string>();
            //去除信息中的所有空格
            msg = msg.Replace(" ", "");

            //将信息按照换行符分割，并去除掉头尾的空白字符
            var listMsg = msg.Split('\n');

            //继续将信息按照冒号分割成键值对
            for (int i = 0; i < listMsg.Length; i++)
            {
                var temp = listMsg[i].Split(':');

                //获取key和value
                string key = null;
                string value = null;

                if (temp.Length == 2)
                {
                    key = temp[0].Trim();
                    value = temp[1].Trim();
                }
                else if (temp.Length > 2)
                {
                    key = temp[0].Trim();

                    for (int j = 1; j < temp.Length; j++)
                    {
                        value += temp[j].Trim();
                    }
                }

                if (key == null) continue;

                //增加键值对
                if (dict.ContainsKey(key) == false)
                {
                    dict.Add(key, value);
                }
                else
                {
                    dict.TryGetValue(key, out string keyValue);

                    keyValue += "," + value;
                    dict.Remove(key);
                    dict.Add(key, keyValue);
                }
            }
            return dict;
        }


        #endregion

        private void richTextBoxMsg_TextChanged(object sender, EventArgs e)
        {
            richTextBoxMsg.SelectionStart = richTextBoxMsg.Text.Length;
            richTextBoxMsg.ScrollToCaret();
        }
    }
}
